<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>

<style type="text/css">
    .autocomplete-suggestions {
        border: 1px solid #999;
        background: #FFF;
        overflow: auto;
    }

    .autocomplete-suggestion {
        padding: 2px 5px;
        white-space: nowrap;
        overflow: hidden;
    }

    .autocomplete-selected {
        background: #F0F0F0;
    }

    .autocomplete-suggestions strong {
        font-weight: normal;
        color: #3399FF;
    }

    @-moz-document url-prefix() {
        fieldset {
            display: table-cell;
        }
    }

    .recommendInput {
        width: 84px;
        height: 24px;
    }

</style>
<title>推荐订单数据 - 云超市</title>
<%--<jsp:include page="../../head.jsp"/>--%>

<div class="" id="app">
    <form class="form-inline" style="margin-top: 20px; margin-bottom: 5px;">
        <div class="form-group">
            <input id="custCodeInput" onfocus="this.value=''" class="form-control input-sm" type="text" placeholder="请选择专卖证号">
        </div>
        <div class="form-group">
            <input id="yearInput" class="form-control input-sm" type="text" placeholder="请选择年月">
            <input id="weekInput" onfocus="this.value=''" class="form-control input-sm" type="text" placeholder="请选择周">
        </div>
        <button type="button" class="btn btn-sm btn-success" @click="getOrder();">查询</button>
        <button type="button" class="btn btn-sm btn-primary" @click="saveData('','','',[]);">保存</button>
        <button type="button" class="btn btn-sm btn-warning" @click="saveDataAs()">另存</button>
        <button type="button" class="btn btn-sm btn-info" @click="addRecommend()">添加</button>
        <%--<div class="form-group">
            <select class="form-group" v-model="mothRemainWeek">
                <option value="0" disabled>月所剩周</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>
        <button type="button" class="btn btn-sm btn-success" @click="computeRecommed();" title="默认计算推荐量为零的数据，如需计算推荐量不为零的数据请选中该数据">计算推荐量</button>--%>
        <div class="form-group" style="padding-left: 20px;">
            <p id="p1"></p>
            <p id="p2">本月所剩周数（包括本周）：{{mothRemainWeek}}， &nbsp;&nbsp;&nbsp;&nbsp;页面总推荐量：<span style="color: red">{{pageTotalRN}}</span></p>
        </div>
    </form>

    <div class="row" style="background-color: rgb(53, 147, 202);margin-right: 0px;margin-left: 0px; height: 37px;">
        <div class="col-sm-10" style="margin-top: 6px;">
            <strong style="font-size: 18px;color: #f9f9f9;">推荐订单数据列表</strong>
        </div>
        <div class="col-sm-2" style="text-align: right">

        </div>
    </div>
    <div class="table-responsive" id="tableContainer" style="height: 365px">
        <table class="table table-striped table-condensed table-bordered">
            <thead style="background-color:#eee;">
            <tr>
                <th><button @click="clickAllSB();">{{isAllS ? '全不选' : '全选'}}</button></th>
                <th>专卖证号</th>
                <th>客户名称</th>
                <th>烟品编码</th>
                <th>烟品名称</th>
                <th>年月</th>
                <th>本月已购量(条)</th>
                <th>月同期数量(条)</th>
                <th>同期增长率</th>
                <th>月上期数量(条)</th>
                <th>上期增长率</th>
                <th>周</th>
                <th>推荐数量(条)</th>
                <%--<th>操作时间</th>--%>

            </tr>
            </thead>
            <tbody>

            <tr v-for="(order, index) in orderList">
                <td><input type="checkbox" v-model="order.CHECK"></td>
                <td>{{order.CUST_CODE}}</td>
                <td>{{order.CUST_NAME}}</td>
                <td>{{order.BAR_CODE}}</td>
                <td>{{order.PRODUCT_NAME}}</td>
                <td>{{order.YEAR_MOTH}}</td>
                <td>{{order.CURRENT_ALREADY}}</td>
                <td>{{order.TCP_NUM}}</td>
                <td>{{order.GROWTH_RATE}}</td>
                <td>{{order.LAST_NUM}}</td>
                <td>{{order.LAST_GR}}</td>
                <td>{{order.YEAR_WEEK}}</td>
                <td onclick="clickRecommendNum(this);">
                    <span>{{order.RECOMMEND_NUM}}</span>
                    <input min="0" onclick="stopD()" onblur="$(this).parent().click()" type="number" class="recommendInput hide" v-model="order.RECOMMEND_NUM">
                </td>
              <%--  <td>{{order.OPERATOR_TIME}}</td>--%>
            </tr>
            </tbody>
        </table>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" data-backdrop="static" role="dialog"
         aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">推荐订单数据添加</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="add_cust_code" placeholder="请选择客户">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">品规：</label>
                            <div class="col-sm-8">
                                <select class="form-control" id="add_bar_code">
                                    <option value="">——请选择品规——</option>
                                    <option v-for="prod in prods" :value="prod.data">
                                        {{prod.value}}
                                    </option>
                                </select>
                               <%--<input class="form-control" id="add_bar_code" placeholder="请输入烟品条条码">--%>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">年月：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="add_year" placeholder="请选择年月">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">周：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="add_week" placeholder="请选择周">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">推荐数量：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="add_rn" placeholder="输入推荐数量">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" @click="submitData">确定</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 另存为modal -->
    <div class="modal fade" id="saveAsModal" tabindex="-1" data-backdrop="static" role="dialog"
         aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="saveAsModalLabel">另存为</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="saveAs_cust_code" placeholder="请选择客户">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">年月：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="saveAs_year" placeholder="请选择年月">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">周：</label>
                            <div class="col-sm-8">
                                <input class="form-control" id="saveAs_week" placeholder="请选择周">
                            </div>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" @click="saveAsData">确定</button>
                </div>
            </div>
        </div>
    </div>

</div>
<script src="${contextPath}/static/statistics/js/vue.js"></script>
<script type="text/javascript" src="${contextPath}/static/statistics/js/jquery.autocomplete.js"></script>
<script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.min.js"></script>
<script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.fr.js"></script>
<script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">
    var laypage = layui.laypage
        , layer = layui.layer;

    var app = new Vue({
        el: '#app',
        data: {
            isAllS : false,
            orderList: [],
            customers: [],
            prods: [],
            mothRemainWeek : '3',
            select: {
                custCode : '',
                year: '',
                week : '',
            },
            saveAsDatas:[],
            pageTotalRN : '',
            ymw : null,
        },
        methods: {
            getInitData: function () {
                var vm = this;
                $.ajax({
                    url: '${contextPath}/shop/getRecommendCustAndProdData',
                    type: 'get',
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        if (data.success) {
                            vm.customers = data.data.customers;
                            vm.prods = data.data.prods;
                            $('#custCodeInput').autocomplete({
                                lookup: vm.customers,
                                minChars: 0,
                            });
                            $('#add_cust_code').autocomplete({
                                lookup: vm.customers,
                                minChars: 0,
                            });
                            $('#saveAs_cust_code').autocomplete({
                                lookup: vm.customers,
                                minChars: 0,
                            });

                            vm.mothRemainWeek = data.data.mothRemainWeek ? data.data.mothRemainWeek  : 0;
                            vm.ymw = data.data.ymw;
                        } else {
                            layer.msg('获取客户和烟品信息失败失败');
                        }

                    },
                    error: function (error) {
                        if (window.console) console.error(error);
                        layer.msg('网络错误');
                    }
                });

       /*         $('#yearInput').autocomplete({
                    lookup: [{data:'2017',value:'2017'},{data:'2018',value:'2018'}],
                    minChars: 0,
                });*/

                var weeks = []
            /*    for (var i = 1 ;i <= 6 ;i++) {
                    weeks.push({data:i+'',value:i+''})
                }*/
/*                $('#weekInput').autocomplete({
                    lookup: weeks,
                    minChars: 0,
                });

                $('#add_week').autocomplete({
                    lookup: weeks,
                    minChars: 0,
                });
                $('#saveAs_week').autocomplete({
                    lookup: weeks,
                    minChars: 0,
                });*/

                $('#yearInput').datetimepicker({
                    language: 'zh-CN',
                    weekStart: 1,
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: 1,
                    startView: 3,
                    forceParse: 0,
                    minView: 3,
                    showMeridian: 1,
                    format: "yyyy-mm",
                });

                $('#yearInput').on('change',function (e) {

                   var value = e.target.value;

                   if (value.length != 7 || value.split('-').length !=2) {
                       $('#weekInput').autocomplete({
                           lookup: weeks,
                           minChars: 0,
                       });
                       return;
                   }

                   layer.load(1);
                    $.ajax({
                        url: '${contextPath}/shop/getYearWeekByYearMonth?yearMonth='+value,
                        type: 'get',
                        dataType: 'json',
                        success: function (data) {
                            layer.closeAll('loading');
                            if (data.success) {
                                var yearWeek = data.data;
                                if (yearWeek.length >0) {
                                    $('#weekInput').autocomplete({
                                        lookup: yearWeek,
                                        minChars: 0,
                                    });
                                }else {
                                    $('#weekInput').autocomplete({
                                        lookup: weeks,
                                        minChars: 0,
                                    });
                                }

                            } else {
                                layer.msg('获取年月的周数失败');
                                $('#weekInput').autocomplete({
                                    lookup: weeks,
                                    minChars: 0,
                                });
                            }

                        },
                        error: function (error) {
                            layer.closeAll('loading');
                            $('#weekInput').autocomplete({
                                lookup: weeks,
                                minChars: 0,
                            });
                            if (window.console) console.error(error);
                            layer.msg('网络错误');
                        }
                    });

                });

             /*   $('#add_year').autocomplete({
                    lookup: [{data:'2017',value:'2017'},{data:'2018',value:'2018'}],
                    minChars: 0,
                });*/
                $('#add_year').on('change',function (e) {

                    var value = e.target.value;

                    if (value.length != 7 || value.split('-').length !=2) {
                        $('#add_week').autocomplete({
                            lookup: weeks,
                            minChars: 0,
                        });
                        return;
                    }

                    layer.load(1);
                    $.ajax({
                        url: '${contextPath}/shop/getYearWeekByYearMonth?yearMonth='+value,
                        type: 'get',
                        dataType: 'json',
                        success: function (data) {
                            layer.closeAll('loading');
                            if (data.success) {
                                var yearWeek = data.data;
                                if (yearWeek.length >0) {
                                    $('#add_week').autocomplete({
                                        lookup: yearWeek,
                                        minChars: 0,
                                    });
                                }else {
                                    $('#add_week').autocomplete({
                                        lookup: weeks,
                                        minChars: 0,
                                    });
                                }

                            } else {
                                layer.msg('获取年月的周数失败');
                                $('#add_week').autocomplete({
                                    lookup: weeks,
                                    minChars: 0,
                                });
                            }

                        },
                        error: function (error) {
                            layer.closeAll('loading');
                            $('#add_week').autocomplete({
                                lookup: weeks,
                                minChars: 0,
                            });
                            if (window.console) console.error(error);
                            layer.msg('网络错误');
                        }
                    });

                });

                $('#saveAs_year').on('change',function (e) {

                    var value = e.target.value;

                    if (value.length != 7 || value.split('-').length !=2) {
                        $('#saveAs_week').autocomplete({
                            lookup: weeks,
                            minChars: 0,
                        });
                        return;
                    }

                    layer.load(1);
                    $.ajax({
                        url: '${contextPath}/shop/getYearWeekByYearMonth?yearMonth='+value,
                        type: 'get',
                        dataType: 'json',
                        success: function (data) {
                            layer.closeAll('loading');
                            if (data.success) {
                                var yearWeek = data.data;
                                if (yearWeek.length >0) {
                                    $('#saveAs_week').autocomplete({
                                        lookup: yearWeek,
                                        minChars: 0,
                                    });
                                }else {
                                    $('#saveAs_week').autocomplete({
                                        lookup: weeks,
                                        minChars: 0,
                                    });
                                }

                            } else {
                                layer.msg('获取年月的周数失败');
                                $('#saveAs_week').autocomplete({
                                    lookup: weeks,
                                    minChars: 0,
                                });
                            }

                        },
                        error: function (error) {
                            layer.closeAll('loading');
                            $('#saveAs_week').autocomplete({
                                lookup: weeks,
                                minChars: 0,
                            });
                            if (window.console) console.error(error);
                            layer.msg('网络错误');
                        }
                    });

                });




                $('#add_year').datetimepicker({
                    language: 'zh-CN',
                    weekStart: 1,
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: 1,
                    startView: 3,
                    forceParse: 0,
                    minView: 3,
                    showMeridian: 1,
                    format: "yyyy-mm",
                });

                $('#saveAs_year').datetimepicker({
                    language: 'zh-CN',
                    weekStart: 1,
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: 1,
                    startView: 3,
                    forceParse: 0,
                    minView: 3,
                    showMeridian: 1,
                    format: "yyyy-mm",
                });
          /*      $('#saveAs_year').autocomplete({
                    lookup: [{data:'2017',value:'2017'},{data:'2018',value:'2018'}],
                    minChars: 0,
                });*/

                //计算当月所剩周
               // var now = new Date()
               // var date = now.getDate();
               // var w = Math.ceil((30 - date)/7);
               // this.mothRemainWeek = w;



                if (localStorage.custCode) {
                    $("#custCodeInput").val(localStorage.custCode);
                    $("#yearInput").val(localStorage.yearMoth);

                    var p1Text = '客户月度推荐量：' + localStorage.rn
                    + '，已购量：' + localStorage.cn + '，剩余量：'
                        +  ((new Number(localStorage.rn) - new Number(localStorage.cn)) >0 ? (new Number(localStorage.rn) - new Number(localStorage.cn)) : 0)
                        + '，每周建议量：' + (this.mothRemainWeek ?  Math.ceil(((new Number(localStorage.rn) - new Number(localStorage.cn)) > 0 ? (new Number(localStorage.rn) - new Number(localStorage.cn)) : 0) /this.mothRemainWeek) : 0);
                    $("#p1").html(p1Text);
                    $('#yearInput').change();
                    this.getOrder();
                }



            },
            submitData: function () {
                var custCode = $("#add_cust_code").val();
                var barCode = $("#add_bar_code").val();
                var year = $("#add_year").val();
                var week = $("#add_week").val();
                var rn = $("#add_rn").val();

                if (!custCode) {
                    layer.msg('请选输入客户号');
                    return;
                }
                if (!barCode) {
                    layer.msg('请选择烟品品规');
                    return;
                }
                if (!year) {
                    layer.msg('请输入年月');
                    return;
                }
                if (!week) {
                    layer.msg('请输入周');
                    return;
                }
                if (!rn) {
                    layer.msg('请输入推荐量');
                    return;
                }
/*                if (week.length==1) {
                    week = '0'+week
                }*/

                if(!this.checkDateIsLegal(year,week)) {
                    layer.msg('不能添加之前时间的数据')
                    return;
                }
                layer.load(1);
                $.ajax({
                    url: '${contextPath}/shop/addRecommendData',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        stringMap: {
                            custCode : custCode,
                            barCode : barCode,
                            year : year,
                            week : week,
                            rn : rn,
                        }
                    },
                    //async : false,
                    success: function (data) {
                        layer.closeAll('loading');
                        layer.msg(data.message);
                        if (data.success) {
                            $('#myModal').modal('hide')
                        }

                    },
                    error: function (error) {
                        layer.closeAll('loading');
                        if (window.console) console.error(error);
                        layer.msg('网络错误');
                    }
                });


            },
            getOrder: function () {
                var vm = this;

                var custCode = $("#custCodeInput").val();
                if (!custCode) {
                    layer.msg('请选择专卖证号');
                    return;
                }
                var year = $("#yearInput").val();
                if (!year) {
                   //layer.msg('请选择年');
                   //return;
                }
                var week = $("#weekInput").val();
                if (!week) {
                    //layer.msg('请选择年周');
                    //return;
                }
             /*   if (week.length==1) {
                    week = '0'+week
                }*/
                if (custCode == localStorage.custCode && year == localStorage.yearMoth) {
                    $('#p1').removeClass('hide')
                }else {
                    $('#p1').addClass('hide')
                }

                layer.load(1);
                $.ajax({
                    url: '${contextPath}/shop/getRecommendOrderList',
                    type: 'post',
                    data: {
                        stringMap: {
                            custCode: custCode,
                            year: year,//year.length>4 && week ? year.substring(0,4) : year,
                            week: week,
                        },
                    },
                    success: function (data) {
                        layer.closeAll('loading')
                        if (data.success == false) {
                            layer.msg("获取出错：" + data.message)
                            return
                        }
                        //console.log(data)
                        var rData = data.data;
                        if (rData.length == 0) {
                            layer.msg("查询没有数据")
                        }
                        app.orderList = rData;
                        app.computePageTotalRN();
                        app.select.custCode = custCode;
                   /*     if (year.length >4) {
                            year = year.substring(0,4);
                        }*/
                        app.select.year = year;
                        app.select.week = week;
                        app.isAllS = false;
                    },
                    error: function (error) {
                        layer.closeAll('loading');
                        layer.msg('网络错误');
                        if (window.console) console.log(error)
                    }
                })
            },
            revocation: function (index) {
                var indx = layer.confirm('确定要撤销吗？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    console.log(app.orderList[index])
                    layer.close(indx);
                    layer.load(1)
                    //修改订单状态
                    $.ajax({
                        url: '${contextPath}/shop/revocationCourier',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            stringMap: app.orderList[index]
                        },
                        success: function (data) {
                            layer.closeAll('loading');
                            layer.msg(data.message);
                            if (data.success) {
                                app.getOrder();
                            }
                        },
                        error: function (error) {
                            layer.msg("网络错误");
                            layer.closeAll('loading')
                        }
                    });
                }, function () {
                });
            },
            clickAllSB : function () {
                this.isAllS = !this.isAllS;
                for (var index in this.orderList) {
                    this.orderList[index].CHECK = this.isAllS
                }

            },
            saveData : function (custCode,year,week,saveDs) {
                if (saveDs.length == 0) { //处理保存的数据
                    for (var index in this.orderList) {
                        var order = this.orderList[index];
                        if (order.CHECK) {
                            if (order.YEAR_WEEK && this.checkDateIsLegal(order.YEAR_MOTH,order.YEAR_WEEK)) {
                                    saveDs.push(order)
                            } else {
                                if (this.select.week && this.checkDateIsLegal(order.YEAR_MOTH,this.select.week)) { //this.select.year &&
                                    var w = this.select.week;
                                   /* if (w.length==1) {
                                        w = '0'+w
                                    }*/
                                    //var yearWeek = this.select.year + '-' +w;
                                    order.YEAR_WEEK = w;
                                    saveDs.push(order);
                                }
                            }
                        }
                    }
                }

                if (saveDs.length == 0) {
                    layer.msg('请至少选择一条需要保存的合法数据')
                    return;
                }
                layer.load(1);
              /*  if (week.length==1) {
                    week = '0'+week
                }*/
                $.ajax({
                    url: '${contextPath}/shop/saveRecommendData',
                    type: 'post',
                    data: {
                        data : JSON.stringify(saveDs),
                        year : year,
                        week : week,
                        custCode : custCode,
                    },
                    success: function (data) {
                        layer.closeAll('loading')
                        if (!data.success) {
                            layer.msg("保存出错：" + data.message)
                            return
                        }
                        layer.msg('保存成功');
                        $('#saveAsModal').modal('hide');

                    },
                    error: function (error) {
                        layer.closeAll('loading');
                        layer.msg('网络错误');
                        if (window.console) console.log(error)
                    }
                })
            },
            saveDataAs :function () {


                var saveDs = []; //另存为的数据
                for (var index in this.orderList) {
                    var order = this.orderList[index];
                    if (order.CHECK) {
                        for (var i in saveDs) {
                            var saveD = saveDs[i];
                            if (order.BAR_CODE == saveD.BAR_CODE) {
                                layer.msg('烟品：'+ (order.PRODUCT_NAME ? order.PRODUCT_NAME : '') + '('+ order.BAR_CODE + ')' + '重复选择，请只选择一条');
                                return;
                            }
                        }
                        saveDs.push(order)
                    }
                }
                if (saveDs.length == 0) {
                    layer.msg('请至少选择一条需要另存的数据')
                    return;
                }
                this.saveAsDatas = saveDs;
                $('#saveAs_cust_code').val(this.select.custCode);
                $('#saveAs_year').val(this.select.year);
                $('#saveAs_year').change();
                $('#saveAsModal').modal('show');
            },
            saveAsData : function () {
                var custCode = $("#saveAs_cust_code").val();
                if (!custCode) {
                    layer.msg('请选择专卖证号');
                    return;
                }
                var year = $("#saveAs_year").val();
                if (!year) {
                    layer.msg('请选择年月');
                    return;
                }
                var week = $("#saveAs_week").val();
                if (!week) {
                    layer.msg('请选择周');
                    return;
                }

                if (!this.checkDateIsLegal(year,week)) {
                    layer.msg('另存年月周不能为当前之前');
                    return;
                }

                var title = "<div>将列表中选中品规推荐量存到</div>" + "<div>客户：" + "<span style='color: #0aa5df'>" +custCode + "</span>" + "</div>"   + "<div>" +  "<span style='color: #0aa5df'>" + year +"</span>" + "年月，第" +  "<span style='color: #0aa5df'>" + week +"</span>" + "周" + "</div>";

                var indx = layer.confirm(title, {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    layer.close(indx);
                    app.saveData(custCode,year,week,app.saveAsDatas);
                }, function () {
                });
            },
            computeRecommed : function () {
                layer.load(1);
                //同期 * 增长率 - 已购 ／ 所剩周数
                var resultList = []
                for (var index in this.orderList) {
                    var order = this.orderList[index];
                    if (order.CHECK || !order.RECOMMEND_NUM || order.RECOMMEND_NUM == '0') {
                        var ca = order.CURRENT_ALREADY ? new Number(order.CURRENT_ALREADY) : 0;
                        if (order.TCP_NUM &&  order.GROWTH_RATE) {
                            var rn = ((new Number(order.TCP_NUM) * new Number(order.GROWTH_RATE)) - ca) / new Number(app.mothRemainWeek);
                            var n = Math.ceil(rn);
                            if (n <=0) {
                                n = 0
                            }
                            order.RECOMMEND_NUM = n;
                        }
                    }
                    resultList.push(order)
                }
                this.orderList = resultList
                layer.closeAll('loading');
            },
            addRecommend : function () {
                var custCode = $("#custCodeInput").val();
                $("#add_cust_code").val(custCode);

                var year = $("#yearInput").val();
                /*if (year.length >4) {
                    year = year.substring(0,4);
                }*/
                $("#add_year").val(year);

                var week = $("#weekInput").val();
                $("#add_week").val(week);
                $('#add_year').change();
                $('#myModal').modal('show')
            },
            computePageTotalRN : function () {
                var totalN = 0 ;
                for(var index in this.orderList) {
                    var order = this.orderList[index];
                    if(order.RECOMMEND_NUM) {
                        totalN += new Number(order.RECOMMEND_NUM)
                    }
                }
                this.pageTotalRN = totalN;
            },
            checkDateIsLegal : function (yearMonth,week) {
                if (!this.ymw) {
                    return false;
                }

                var y = this.ymw.YEAR_MONTH.substr(0,4);
                var m = this.ymw.YEAR_MONTH.substr(5,7);
                var w = this.ymw.WEEK;

                var y1 = yearMonth.substr(0,4);
                var m1 = yearMonth.substr(5,7);

                if (y1 < y) {
                    return false;
                }

                if (y1 == y && m1 < m) {
                    return false;
                }

                if (y1 == y && m1 == m && week <= w) {
                    return false;
                }

                return true;
            }
        },
        watch: {

        }
    })
    app.getInitData();
    //app.getOrder();


    function clickRecommendNum(vm) {
        var $span = $(vm).children("span:first-child");
        var $input = $(vm).children("input:last-child");
        if ($span.hasClass("hide")) {
            $span.removeClass("hide")
            $input.addClass("hide")
            app.computePageTotalRN();
        } else {
            $span.addClass("hide")
            $input.removeClass("hide");
            $input.focus();
        }
    }

    function stopD() {
        cancelBubble();
    }

    function cancelBubble(e) {
        var evt = e ? e : window.event;
        if (evt.stopPropagation) {        //W3C
            evt.stopPropagation();
        } else {       //IE
            evt.cancelBubble = true;
        }
    }
</script>


